package com.xm.xmds1705083.service.util;

import com.xm.xmds1705083.service.mapper.DuoduoDblogMapper;
import com.xm.xmds1705083.service.model.DuoduoDblog;

import java.io.*;
import java.net.Socket;
import java.sql.*;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @author Loysen
 * @company 杭州新苗网络科技有限公司
 * @date 2017/10/10  14:26
 * @
 */
public class ThreadSock  extends Thread {
    Socket socket=null;
    DuoduoDblogMapper duoduoDblogMapper;

    public ThreadSock(Socket socket) {
        this.socket = socket;
    }
    @Override
    public void run() {
        InputStream is=null;
        //提高字符流，提高读取性能
        InputStreamReader reader=null;
        //采用缓冲加快读取速度；
        BufferedReader br=null;
        //回复客服端的相应；
        OutputStream os=null;
        PrintWriter pr=null;
        try {
            is = socket.getInputStream();
            reader = new InputStreamReader(is,"utf-8");
            br = new BufferedReader(reader);
            String len=null;
            String lens1="";
            while((len=br.readLine())!=null){
               System.out.println("服务端：客服端说"+len);
                lens1+=len;
            }
            int c=jdbcutil.get_crc16( lens1.getBytes(),lens1.getBytes().length);
            System.out.print( c+"bbbbbbbbbbbbbb");
      /*     String lens2=  lens1.substring(t,w-t);*/
            socket.shutdownInput();
            Connection conn=null;
            ResultSet rs=null;
            PreparedStatement qstmt=null;
            conn=jdbcutil.getConection();

            String sql="INSERT INTO duoduo_dblog (MSGS1) VALUES (?)";
            conn=jdbcutil.getConection();
         try {
              qstmt=conn.prepareStatement( sql );
              qstmt.setString( 1,lens1 );
              qstmt.executeUpdate();
               } catch (SQLException e) {
                e.printStackTrace();
               }catch (Exception e) {
                e.printStackTrace();
               }finally {
             jdbcutil.close( conn);//关闭数据

           }



            os = socket.getOutputStream();
            pr = new PrintWriter(os);
            pr.write("服务台说：用户登录成功..................");
            pr.flush();
        } catch (IOException e) {
            e.printStackTrace();
        }finally{
            try {
                if (pr!=null)
                    pr.close();
                if (os!=null)
                    os.close();
                if (br!=null)
                    br.close();
                if (reader!=null)
                    reader.close();
                if (is!=null)
                    is.close();
                if (socket!=null)
                    socket.close();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }

    }



}

